package com.usian.controller;

import com.usian.feign.ItemServiceFeign;
import com.usian.pojo.TbItem;
import com.usian.utils.PageResult;
import com.usian.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@Api("商品管理接口")
public class TbItemController {

    @Autowired
    ItemServiceFeign itemServiceFeign;

    @GetMapping("/backend/item/selectTbItemAllByPage")
    //接口的名称和作用
    @ApiOperation(value = "分页查询商品信息",notes = "根据页码查询")
    @ApiImplicitParams({@ApiImplicitParam(name="page", type = "Integer",value = "页码",defaultValue = "1"),
            @ApiImplicitParam(name="rows", type = "Integer",value = "每页多少条",defaultValue = "2")
    })
    public Result selectTbItemAllByPage(@RequestParam(value = "page",defaultValue = "1") Integer page, @RequestParam(value = "rows",defaultValue = "2")Integer rows){
        PageResult pageResult =  itemServiceFeign.selectTbItemAllByPage(page,rows);
        //代码的健壮性
        if (pageResult==null || pageResult.getResult().size()==0){
            return Result.error("查询失败！");
        }
        return Result.ok(pageResult);
    }


    /**
     * 添加商品信息
     */
    @RequestMapping("/backend/item/insertTbItem")
    public Result insertTbItem(TbItem tbItem, String desc, String itemParams){
        Integer num =  itemServiceFeign.insertTbItem(tbItem,desc,itemParams);

        //如果是三个参数代表查询查询
        if (num==3){
            return Result.ok();
        }
            return Result.error("添加失败");
    }

    /**
     * 根据itemId回显商品信息
     */
    @PostMapping("/backend/item/preUpdateItem")
    public Result preUpdateItem(Long itemId){
        Map<String,Object> map = itemServiceFeign.preUpdateItem(itemId);
        if (map==null){
            return Result.error("查询失败!");
        }
            return Result.ok(map);
    }

    /**
     * 修改商品信息
     */
    @GetMapping("/backend/item/updateTbItem")
    public Result updateTbItem(TbItem tbItem, String desc, String itemParams){
        Integer num =  itemServiceFeign.updateTbItem(tbItem,desc,itemParams);

        if (num==3){
            return Result.ok();
        }
            return Result.error("修改失败");
    }


    /**
     * 删除商品信息
     */
    @PostMapping("/backend/item/deleteItemById")
    public Result deleteItemById(Long itemId){
        Integer num = itemServiceFeign.deleteItemById(itemId);

        if(num==1){
            return Result.ok();
        }
            return Result.error("删除失败！");
    }


    /**
     * 添加商品规格模板
     *
     */
    @RequestMapping("/backend/itemParam/insertItemParam")
    public Result insertItemParam(Long itemCatId,String paramData){
       Integer num =  itemServiceFeign.insertItemParam(itemCatId,paramData);
            if(num==1){
            return Result.ok();
         }
            return Result.error("添加失败");
    }

    /**
     * 查询商品规格
     */
    @GetMapping("/backend/itemParam/selectItemParamAll")
    public Result selectItemParamAll(@RequestParam(defaultValue = "1")Integer page,@RequestParam(defaultValue = "10")Integer rows){
       PageResult pageResult = itemServiceFeign.selectItemParamAll(page,rows);
        if (pageResult.getResult().size()>0){
            return Result.ok(pageResult);
        }
            return Result.error("查询失败");
    }

    /**
     * 删除商品规格
     */
    @RequestMapping("/backend/itemParam/deleteItemParamById")
    public Result deleteItemParamById(Long id){
       Integer num =  itemServiceFeign.deleteItemParamById(id);
       if (num==0){
           return Result.error("删除失败");
       }
            return Result.ok();
    }
}
